{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Empty(Exception):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], 'abc', 5]\n",
      "['abc', 5]\n"
     ]
    }
   ],
   "source": [
    "class LinkedStack:\n",
    "    class _Node:\n",
    "        __slots__ = '_element','_next'\n",
    "        def __init__(self,element,next):\n",
    "            self._element = element\n",
    "            self._next = next\n",
    "    def __init__(self):\n",
    "        self._head = None\n",
    "        self._size = 0\n",
    "    def __len__(self):\n",
    "        return self._size\n",
    "    def is_empty(self):\n",
    "        return self._size == 0\n",
    "    def push(self,e):\n",
    "        node = self._Node(e,self._head)\n",
    "        self._head = node\n",
    "        self._size +=1\n",
    "    def top(self):\n",
    "        if self.is_empty():\n",
    "            raise Empty('Stack is empty')\n",
    "        return self._head._element\n",
    "    def pop(self):\n",
    "        if self.is_empty():\n",
    "            raise Empty('Stack is empty')\n",
    "        answer = self._head._element\n",
    "        self._head = self._head._next\n",
    "        self._size -=1\n",
    "        return answer\n",
    "    \n",
    "    def __repr__(self):\n",
    "        data =[]\n",
    "        walk = self._head\n",
    "        while walk._next is not None:\n",
    "            data.append(walk._element)\n",
    "            walk = walk._next\n",
    "        data.append(walk._element)\n",
    "        return f'{data}'\n",
    "\n",
    "if __name__ =='__main__':\n",
    "    s=LinkedStack()\n",
    "    s.push(5)\n",
    "    s.push('abc')\n",
    "    s.push([1,2,3])\n",
    "    print(s)\n",
    "    \n",
    "    s.pop()\n",
    "    print(s)\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
